<%@taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@include file="../WEB-INF/jspf/controlesessao.jspf"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Alocações</title>

        <link rel="stylesheet" type="text/css" media="screen" href="../css/ui-lightness/jquery-ui-1.8.2.custom.css" />
        <link rel="stylesheet" type="text/css" media="screen" href="../css/ui.jqgrid.css" />
        <script src="../js/jquery.js" type="text/javascript"></script>
        <script src="../js/jquery-ui.js" type="text/javascript"></script>
        <script src="../js/i18n/grid.locale-pt-br.js" type="text/javascript"></script>
        <script src="../js/jquery.jqGrid.min.js" type="text/javascript"></script>
        <script src="../js/alocacao.js" type="text/javascript"></script>

    </head>
    <script>

        $(function(){

            $("#solicitar").click(function(){
                processar(document.form_turma_solicitar);

            });
            $("#voltar").click(function(){
                window.location = 'fm_lista_Alocacoes.jsp?token=${sessionScope.token}';
            });

            $("#finalizar").click(function(){
                document.form_turma_solicitar.action='finalizar.jsp?token=${sessionScope.token}';
                processar(document.form_turma_solicitar);
            });
            $("#devolver").click(function(){
                document.form_turma_solicitar.action='devolver.jsp?token=${sessionScope.token}';
                processar(document.form_turma_solicitar);
            });

        });

    </script>


    <sql:query var="queryComp" dataSource="jdbc/dbo_MicrosoftSQLServer">
        select  m.id_modulo,m.desc_modulo, c.id_componente,desc_componente,
        codigo_componente, r.*
        from Componentes c
        inner join grades g on g.id_componente=c.id_componente
        inner join modulos m on m.id_modulo=g.id_modulo
        inner join reservas r on r.id_modulo=m.id_modulo and r.id_componente=g.id_componente
        where r.id_turma=?
        order by desc_modulo,r.ordem
        <sql:param value="${param.id_turma}"/>
    </sql:query>
    <body>
        <sql:query var="query" dataSource="jdbc/dbo_MicrosoftSQLServer">
            SELECT  * FROM Turmas
            where id_turma=?
            <sql:param value="${param.id_turma}"/>
        </sql:query>

        <c:forEach var="row" items="${query.rows}">
            <label>Código da Turma: ${row.id_turma}</label>
            <p><label class="lbInput">Tipo<em>*</em></label>
                <sql:query var="queryTipo" dataSource="jdbc/dbo_MicrosoftSQLServer">
                    SELECT  *
                    FROM tipoturma
                    order by desc_tipoturma
                </sql:query>
                <select name="id_tipoturma" disabled >
                    <option value="">Selecione uma opção</option>
                    <c:forEach var="rowTipo" items="${queryTipo.rows}">
                        <option value="${rowTipo.id_tipoturma}" ${rowTipo.id_tipoturma==row.id_tipoturma?'selected':''}>${rowTipo.desc_tipoturma}</option>
                    </c:forEach>
                </select>
            </p>
            <p><label class="lbInput">Curso</label>
                <sql:query var="queryCursos" dataSource="jdbc/dbo_MicrosoftSQLServer">
                    SELECT  Cursos.*
                    FROM Cursos
                    order by nome_curso, versao_curso
                </sql:query>
                <select name="id_curso" class="required" disabled>
                    <option value="">Selecione um curso</option>
                    <c:forEach var="curso" items="${queryCursos.rows}">
                        <option value="${curso.id_curso}"  ${curso.id_curso==row.id_curso?'selected':''}>${curso.nome_curso}- ${curso.codigo_curso}-V${curso.versao_curso} - ${curso.carga_horaria}h</option>
                    </c:forEach>
                </select>
            </p>
            <p><label class="lbInput">Período<em>*</em></label>
                <fmt:formatDate var="data_inicio" value="${row.inicio_turma}" pattern="dd/MM/yyyy"/>
                <fmt:formatDate var="data_fim" value="${row.fim_turma}" pattern="dd/MM/yyyy"/>

                <input type="text" disabled id="data_inicio" name="data_inicio" value="${data_inicio}" size="12" maxlength="12" class="required">
                <label>a</label>
                <input type="text" id="data_fim" name="data_fim" value="${data_fim}" size="12" maxlength="12" disabled>

            </p>
            <p><label class="lbInput">Código da Turma</label>
                <input type="text" disabled id="codigo_turma" name="codigo_turma" value="${row.codigo_turma}" size="12" maxlength="12">
            </p>

            <br>

        </c:forEach>
        <br>
        <form name="form" id="form" method="post">

            <input type="hidden" name="id_turma" value="${param.id_turma}">
            <label>Componente</label>
            <select name="id_componente" id="id_componente" onchange="submit()">
                <option value="">Selecione um componente</option>
                <c:forEach var="comp" items="${queryComp.rows}">
                    <option value="${comp.id_componente}" ${comp.id_componente==param.id_componente?'selected':''}>
                        ${comp.desc_modulo} - ${comp.desc_componente}
                    </option>
                </c:forEach>
            </select>
            <input type="hidden" id="token" name="token" value="${sessionScope.token}" />
        </form>

        <table id="list"></table>
        <div id="pager"></div>
        <form name="form_turma_solicitar"  id="form_turma_solicitar" method="post" action="solicitar.jsp">
            <input name="id_turma"  type="hidden" size="5" value="${param.id_turma}">
            <input class="submit" type="button" id="voltar" name="voltar" value="Voltar">
            <input class="submit" type="button" id="finalizar" value="Finalizar">
            <input class="submit" type="button" id="solicitar" value="Solicitar Cadastro">
            <input class="submit" type="button" id="devolver" value="Devolver">
        <input type="hidden" id="token" name="token" value="${sessionScope.token}" /> </form>
        <div id="msg">

        </div>
    </body>
</html>
<script type="text/javascript">
    var lastsel2 ;

    $(function(){
        $("#list").jqGrid({
            url:'fm_getAlocacoes.jsp?token=${sessionScope.token}&id_turma=${param.id_turma}&id_componente=${param.id_componente}',
            datatype: 'xml',
            mtype: 'POST',
            colNames:['Data', 'Hora Inicial','Hora Fim','Sala','Docente','Situação','Otimizado'],
            colModel :[
                {name:'data_alocacao', index:'data_alocacao', width:90,edittype:"text",sorttype:"date",editable:true,sortable:false},
                {name:'hora_inicio', index:'hora_inicio', width:80, align:'center',edittype:"text",editable:true,sortable:false},
                {name:'hora_fim', index:'hora_fim', width:80, align:'center',edittype:"text",editable:true,sortable:false},
                {name:'id_sala', index:'id_sala', width:80, align:'center',edittype:"text",editable:false,sortable:false},
                {name:'matricula', index:'matricula', align:'center', width:150, sortable:false,
                    editable:true,edittype:"select",editoptions:{dataUrl:'../Docentes/fm_getDocentes.jsp?token=${sessionScope.token}&id_componente=${param.id_componente}'},editrules:{required:true}
                },
                {name:'situacao', index:'situacao', align:'center', width:150, edittype:"text",sortable:false,editable:true},
                {name:'otimizado', index:'otimizado', align:'center', width:150, edittype:"select",editable:true,
                    editoptions: { value: "S:Sim;N:Não" } ,
                    sortable:false,editable:true,hidden:true,editrules: {edithidden:true} },
            ],
            onSelectRow: function(id){
                if(id && id!==lastsel2){
                    jQuery('#list').jqGrid('restoreRow',lastsel2);
                    jQuery('#list').jqGrid('editRow',id,true);

                    lastsel2=id;
                }
            },
            pager: '#pager',
            autowidth: 800,
            height: 400,
            rowNum:'auto',
            pgbuttons: false,
            pgtext: false,
            pginput:false,
            sortname: 'id_alocacao',
            onAfterSaveCell: reload(),
            sortorder: 'desc',
            editurl: "update.jsp?token=${sessionScope.token}&id_turma=${param.id_turma}&id_componente=${param.id_componente}",
            caption: 'Alocação'
        });

        jQuery("#list").jqGrid('navGrid','#pager', {add:false,search:false}, //options
        {height:280,width:500,reloadAfterSubmit:true}, // edit options
        {height:280,width:500,reloadAfterSubmit:true}, // add options
        {reloadAfterSubmit:true}, // del options
        {} // search options
    );


    });

    function reload(result) {
        $("#list").trigger('reloadGrid');

    }
</script>
